package cn.zhaotianen.retrofit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import cn.zhaotianen.retrofit.domain.GitHubInfo;
import cn.zhaotianen.retrofit.service.AbstractService;
import cn.zhaotianen.retrofit.service.AppClient;
import cn.zhaotianen.retrofit.service.RequestService;
import cn.zhaotianen.retrofit.utils.MD5Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    private String user="zhaotianen";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final TextView textView= (TextView) findViewById(R.id.textView);


        //不用RxJava
        Retrofit retrofit= AppClient.retrofit();
        RequestService requestService=retrofit.create(RequestService.class);
        Call<GitHubInfo> getUserInfo=requestService.getUserInfo(user);
        getUserInfo.enqueue(new Callback<GitHubInfo>() {
            @Override
            public void onResponse(Call<GitHubInfo> call, Response<GitHubInfo> response) {
                Log.e("System.out",response.body().getAvatar_url());
                textView.setText(response.body().getAvatar_url());
            }

            @Override
            public void onFailure(Call<GitHubInfo> call, Throwable t) {

            }
        });


        final GitHubInfo[] gitHubInfo = new GitHubInfo[1];
        //使用RxJava

        RequestService request=retrofit.create(RequestService.class);
        Observable<GitHubInfo> observable=request.getUser(user);
        observable.subscribeOn(Schedulers.newThread())//请求在新的线程中执行
                .observeOn(Schedulers.io())         //请求完成后在io线程中执行
                .doOnNext(new Action1<GitHubInfo>() {
                    @Override
                    public void call(GitHubInfo resInfo) {
                        //操作
                        if (resInfo!=null){
                            gitHubInfo[0] =resInfo;
                        }


                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//最后在主线程中执行
                .subscribe(new Subscriber<GitHubInfo>() {
                    @Override
                    public void onCompleted() {
                        if (gitHubInfo[0]!=null){
                            Toast.makeText(MainActivity.this,gitHubInfo[0].getLocation(),Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        //请求失败
                    }

                    @Override
                    public void onNext(GitHubInfo userInfo) {
                        //请求成功
                    }
                });

    }
}
